﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.Services.Framework;
using GestionPedago.Services.ES.Modele;
using GestionPedago.Persistence.OPManager;
using Persistence.OPManager;

namespace GestionPedago.Services.Mapping
{
    /// <summary>
    /// Classe de mapping Entity<->ES de l'objet PlanningECFDetail
    /// </summary>
    public class PlanningECFDetailMappeur : IMappeur<PlanningECFDetail, PlanningECFDetailES>
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public PlanningECFDetailMappeur() { }


        /// <summary>
        /// Mapping Entity -> ES
        /// </summary>
        /// <param name="entity">L'entity � mapper</param>
        /// <returns>L'ES obtenu</returns>
        public PlanningECFDetailES Entity2ES(PlanningECFDetail entity)
        {
            PlanningECFDetailES retour = new PlanningECFDetailES();
            retour.IdPlanningECFDetail = entity.IdPlanningECFDetail;
            retour.CodePlanning = entity.CodePlanning;
            retour.CodePlanningECF = entity.CodePlanningECF;
            retour.IdVersionECF = entity.IdVersionECF;
            if (entity.PlanningIndividuelFormation != null)
            {
                retour.CodeStagiaire = entity.PlanningIndividuelFormation.CodeStagiaire;
                retour.NomStagiaire = entity.PlanningIndividuelFormation.Stagiaire.Nom;
                retour.PrenomStagiaire = entity.PlanningIndividuelFormation.Stagiaire.Prenom;
            }
            else
            {
                PlanningIndividuelFormation pif = PlanningIndividuelFormationOPManager.GetPlanningIndividuelFormationByCode(entity.CodePlanning);
                retour.CodeStagiaire = pif.CodeStagiaire;
                retour.NomStagiaire = pif.Stagiaire.Nom;
                retour.PrenomStagiaire = pif.Stagiaire.Prenom;
            }
            retour.CodeContactENI = entity.CodeContactENI;
            if (entity.ContactENI != null)
            {
                retour.ContactENI = new ContactENIMappeur().Entity2ES(entity.ContactENI);
            }

            if (entity.PlanningECF != null)
            {
                retour.PlanningECF = new PlanningECFMappeur().Entity2ES(entity.PlanningECF);
            }
            else
            {

                retour.PlanningECF = new PlanningECFMappeur().Entity2ES(PlanningECFOPManager.GetPlanningECFByCode(entity.CodePlanningECF));
            }
            //retour.PlanningIndividuelFormation = new PlanningIndividuelFormationMappeur().Entity2ES(entity.PlanningIndividuelFormation);
            if (entity.VersionECF != null)
            {
                retour.VersionECF = new VersionECFMappeur().Entity2ES(entity.VersionECF);
            }
            retour.ListeValidationCompetence = new List<ValidationCompetenceES>();
            retour.SuggestionsPromotion = new List<String>();
            retour.SuggestionsPromotion = PromotionOPManager.GetListePromotionsSuggestionPlanification(entity.CodePlanning,entity.PlanningECF.IdECF);
           /* foreach (ValidationCompetence vc in entity.ValidationCompetence)
            {
                retour.ListeValidationCompetence.Add(new ValidationCompetenceMappeur().Entity2ES(vc));
            }*/
            return retour;
        }

        /// <summary>
        /// Mapping ES -> Entity
        /// </summary>
        /// <param name="es">L'ES � mapper</param>
        /// <returns>L'entity obtenu</returns>
        public PlanningECFDetail ES2Entity(PlanningECFDetailES es)
        {
            PlanningECFDetail retour = new PlanningECFDetail();
            retour.IdPlanningECFDetail = es.IdPlanningECFDetail;
            if (es.IdPlanningECFDetail != 0)
            {
                retour = PlanningECFDetailOPManager.GetPlanningECFDetailById(es.IdPlanningECFDetail);
            }
            bool alreadyAttached = !EntityStateChecker.IsDetached(retour);
            if (!alreadyAttached)
            {
                EntityConnection.GetContext().PlanningECFDetail.Attach(retour);
            }
            retour.CodePlanning = es.CodePlanning;
            retour.CodePlanningECF = es.CodePlanningECF;
            retour.IdVersionECF = es.IdVersionECF;
            retour.CodeContactENI = es.CodeContactENI;
            if(es.CodeContactENI != null)
            {
                retour.ContactENI = ContactENIOPManager.GetContactENIByCode(es.CodeContactENI.Value);
            }
            if (es.CodePlanningECF != 0)
            {
                retour.PlanningECF = new PlanningECFMappeur().ES2Entity(es.PlanningECF);
            }

            //retour.PlanningIndividuelFormation = new PlanningIndividuelFormationMappeur().ES2Entity(es.PlanningIndividuelFormation);
            if(es.IdVersionECF != null)
            {
                retour.VersionECF = VersionECFOPManager.GetVersionECFByIdVersionECF(es.IdVersionECF.Value);
            }
            /*foreach (ValidationCompetenceES vc in es.ListeValidationCompetence)
            {
                retour.ValidationCompetence.Add(new ValidationCompetenceMappeur().ES2Entity(vc));
            }*/
            if (alreadyAttached)
            {
                EntityConnection.GetContext().PlanningECFDetail.ApplyCurrentValues(retour);

            }
            return retour;
        }
    }
}
